<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button onclick="store.dispatch({type:'SUB',value:2})">-</button>
  <span id="count">10</span>
  <button onclick="store.dispatch({type:'DESC',value:2})">+</button>
</body>

<script>

  const span = document.querySelector("#count")

  const state = {
    count: 5
  }

  const changeState = (state, action) => {
    switch (action.type) {
      case "DESC":
        return {
          ...state,
          count: state.count + action.value
        }
        break
      case "SUB":
        return {
          ...state,
          count: state.count - action.value
        }
      default:
        state
    }
  }

  // 入口
  function createStore(state, changeState) {
    let listeners = []
    return {
      getState: () => state,
      dispatch: (action) => {
        state = changeState(state, action)
        listeners.forEach(listener => listener())
      },
      subscribe: (listener) => listeners.push(listener)
    }
  }

  const store = createStore(state, changeState)

  const render = () => {
    span.innerHTML = store.getState().count
  }

  store.subscribe(render)

  render()

  function createdStore(state, changeState) {
    let listeners = []
    return {
      getState: () => state,
      dispacth: (action) => {
        state = changeState(state, action)
        listeners.forEach(listener => listener())
      },
      subscribe: (listener) => listeners.push(listener)
    }

  }

</script>

</html>